﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class ProductFrm : Form
    {
        private ClothContext db = new ClothContext();
        private int _productType = 0;
        private bool isNew = true;
        private string _productno = "";
        public ProductFrm(int productType)
        {
            InitializeComponent();
            _productType = productType;
            if (productType > 0)
            {
                productTypeLab.Text = "进货商品";
            }
            else productTypeLab.Text = "销售商品";

        }
        public ProductFrm(string productno)
        {
            InitializeComponent();
            isNew = false;
            var product = db.Products.AsNoTracking().Where(o => o.ProductNo == productno).FirstOrDefault();
            if (product != null)
            {
                productNoTxt.Text = product.ProductNo;
                priceTxt.Text = product.Price.ToString();
                batchpriceTxt.Text = product.BatchPrice.ToString();
            }
            _productno = product.ProductNo;
            productNoTxt.ReadOnly = true;
            unitCbb.Hide();
            unitLab.Hide();
            
            //_productType = productType;
            //if (productType > 0)
            //{
            //    productTypeLab.Text = "进货商品";
            //}
            //else productTypeLab.Text = "销售商品";

        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (isNew)
            {
                if (_productType == 0)
                {
                    if (db.Products.Where(o => o.ProductNo.Contains(productNoTxt.Text)).Count() > 0)
                    {
                        MessageBox.Show("该商品已经存在！");
                        return;
                    }
                    var product = new Product()
                    {
                        Price = priceTxt.Text == "" ? 0 : Convert.ToDecimal(priceTxt.Text),
                        BatchPrice = batchpriceTxt.Text == "" ? 0 : Convert.ToDecimal(batchpriceTxt.Text),
                        ProductNo = productNoTxt.Text,
                        CreateTime = DateTime.Now,
                        Unit = unitCbb.SelectedValue != null ? (int)unitCbb.SelectedValue : 0,
                        Enabled = true
                    };
                    db.Products.Add(product);
                    var result = db.SaveChanges();
                    if (result > 0)
                    {
                        MessageBox.Show("保存成功！");

                        //this.Hide();
                    }

                }
                else
                {
                    //var product = new InStockProduct()
                    //{
                    //    Price = priceTxt.Text == "" ? 0 : Convert.ToDecimal(priceTxt.Text),
                    //    InstockNo = productNoTxt.Text,
                    //    CreateTime = DateTime.Now,
                    //    Unit = unitCbb.SelectedValue != null ? (int)unitCbb.SelectedValue : 0,
                    //};
                    //db.InStockProducts.Add(product);
                    //var result = db.SaveChanges();
                    //if (result > 0)
                    //{
                    //    MessageBox.Show("保存成功！");
                    //}

                }
            }
            else
            {
                try
                {
                    var product = db.Products.Where(o => o.ProductNo == _productno).FirstOrDefault();
                    product.Price = Convert.ToDecimal(priceTxt.Text);
                    product.BatchPrice = Convert.ToDecimal(batchpriceTxt.Text);
                    if (db.SaveChanges() > 0)
                    {
                        MessageBox.Show("保存成功！");
                        this.Hide();
                       
                       
                    };
                }
                catch (Exception err)
                {
                    MessageBox.Show("出现错误：" + err.ToString());
                }
            }
        }

        private void ProductFrm_Load(object sender, EventArgs e)
        {

        }
    }
}
